from dataclasses import field
from ensurepip import bootstrap
from django.shortcuts import render
from app01 import models
from app01.utils.pagination import Pagination
from django import forms
from django.core.validators import ValidationError
from django.shortcuts import render,redirect
from django.http import HttpResponse,JsonResponse
from app01.utils.bootstrape import BootStrapModelForm,BootStrapForm
from django.views.decorators.csrf import csrf_exempt
from datetime import datetime
import random




class ClientModelForm(BootStrapModelForm):
    class Meta:
        model=models.Client
        fields="__all__"
def client(request):
    queryset=models.Client.objects.all().order_by("-id")
    form=ClientModelForm()
    page_obj=Pagination(request,queryset)
    contxt={'queryset':page_obj.page_queryset,
        'page_string':page_obj.html(),
        'form':form
    }
    return render(request,'client.html',contxt)

class ClientAddModelFrom(BootStrapModelForm):
    class Meta:
        model=models.Client
        fields=['cname','caddr','cmobile']

    def clean_mobile(self):
        txt_mobile=self.cleaned_data['cmobile']
       
        if len(txt_mobile) != 11:
            raise ValidationError('格式错误')
        return txt_mobile

def client_add(request):
    if request.method=='GET':
        form=ClientAddModelFrom()  
        return render(request,'client_add.html',{'form':form})
    form=ClientAddModelFrom(data=request.POST)
    if form.is_valid():
        form.save()
        s=form
        return redirect('/client/')
    return render(request,'client_add.html',{'form':form})

class ClientReserveModelForm(BootStrapModelForm):
    class Meta:
        model=models.Client
        fields="__all__"

@csrf_exempt  
def reserve_water(request):
    if request.method=='GET':
        obj=models.Purify_water.objects.all()
        return render(request,'reserve_water.html',{'obj':obj})
    nid=request.GET.get('test')
    print(nid)
    Num='num'+nid
    print(Num)
    num=request.GET.get(Num)
    print(num)
    price=request.GET.get(nid)
    take=int(num)*int(price)
    # print(take)
    # row_obj=models.Purify_water.objects.filter(id=int(nid)-1)
    # print(row_obj)
    # # return render(request,'order_detail.html',{"row_obj":row_obj})
    
@csrf_exempt   
def reserve_result(request):
   
    oid=datetime.now().strftime("%Y%m%d%H%M%S")+str(random.randint(1000,9999))
    nidlist=request.GET.getlist('test')
    s=models.Client.objects.all().last()
    cname=s.cname
    cid=s.id
    s.Purify_waters.set(nidlist)
    take=0
    num=0
    for i in nidlist:
        obj=models.Purify_water.objects.filter(id=i).first()
        price=obj.price
        pname=obj.pname
        Num='num'+i
        num=(request.GET.get(Num))
        
        take=(int(price)*int(num))
       
    num1=models.Purify_water.objects.filter(id=i).first().num
    models.Purify_water.objects.filter(id=i).update(num=num1-int(num))
    models.Order.objects.create(cname=cname,pname=pname,oid=oid,num=num,take=take)
   
    return JsonResponse({"status":True})
    
def order_detail(request):
    uid=request.GET.get("status")
    print(uid)
    water=models.Purify_water.objects.get(id=uid)
    uid=water.Client_set.all().last().get("uid")

    if models.Order.objects.filter(cid=uid):
       
        models.Order.objects.filter(cid=uid).update(state=2)
    return JsonResponse({"status":True})

def client_order(request):
    queryset=models.Order.objects.all().last()
    print(queryset.oid)
    return render(request,'client_order.html',{'queryset':queryset})